/**
 * Name:
 * Desc:
 * Author: Administrator
 * Time: 13-1-23 下午12:50
 */
function checkForm() {
    var $comment = KISSY.one("#J_Comment");
    var $submit = KISSY.one("#J_Submit");
    if ($submit.hasClass("submit-disable")) {
        return false
    }
    if (!$comment.val().replace(/(^\s*)|(\s*$)/g, "")) {
        $comment.animate({
                backgroundColor: "rgb(255,105,74)"
            },
            .5, "easeIn",
            function() {
                $comment.animate({
                        backgroundColor: "rgb(255,255,255)"
                    },
                    .5, "easeOut")
            });
        return false
    } else {
        $submit.val("发布中").addClass("submit-disable");
        $comment.attr("readonly", "readonly");
        return true
    }
}
KISSY.ready(function(S) {
    var $ = S.Node.all;
    S.use("cookie,anim,switchable,sizzle",
        function(S, Cookie, Anim, Switchable) {
            function supportMediaQuery() {
                var ie6Promot = ["A higher browser will be suited to our site!", "消灭IE6，从我做起！", "You deserve a better experience!", "You will got a terrible experience!", "It's time to change!", "Please say goodbye to IE6!", "Still using IE6? oh, that's pathetic!"];
                if (S.UA.shell == "ie" && S.UA.ie <= 7) {
                    var toDisplay = ie6Promot[Math.floor(ie6Promot.length * Math.random())];
                    alert(toDisplay)
                }
                if (S.UA.shell == "firefox" && S.UA.firefox < 3.5 || S.UA.shell == "ie" && S.UA.ie < 9) return false;
                else return true
            }
            supportMediaQuery();
            function promot(msg) {
                $("#J_Promot .promot-content").html(msg);
                $("#J_Promot").fadeIn();
                setTimeout(function() {
                        $("#J_Promot").fadeOut()
                    },
                    3e3)
            }
            function readCookies(post_id) {
                return Cookie.get("LIKE_POST_" + post_id)
            }
            function writeCookies(post_id, flag) {
                var date = new Date((new Date).getTime() + 30 * 24 * 60 * 60 * 1e3);
                var str = "LIKE_POST_" + post_id + "=" + flag + "; expires=" + date.toGMTString();
                document.cookie = str
            }
            function resetNavItem(firstTime) {
                var cur, curTop;
                if ($("#J_CatID").length != 0) {
                    var selector = ".cat-item-" + $("#J_CatID").val();
                    $(selector).addClass("current-cat");
                    cur = $(selector)
                } else {
                    cur = $(S.get(".cat-item"));
                    if ($(".current-cat").length) {
                        cur = $(".current-cat")
                    }
                    if ($(".current-menu-item").length) {
                        cur = $(".current-menu-item")
                    }
                }
                if (firstTime) {
                    if ($("#J_Slide").length == 0) {
                        curTop = cur.offset().top - $("#nav").offset().top - 100;
                        $("#J_CurrentBG").css("top", curTop).fadeIn()
                    } else {
                        var oldTop = $("#nav").css("top");
                        $("#nav").css("top", "-500px").show();
                        $("#nav").animate({
                                top: oldTop
                            },
                            1, "easeOutStrong",
                            function() {
                                curTop = cur.offset().top - $("#nav").offset().top - 100;
                                $("#J_CurrentBG").css("top", curTop).fadeIn()
                            },
                            true)
                    }
                } else {
                    curTop = cur.offset().top - $("#nav").offset().top - 100;
                    hoverNavItem(curTop, .3)
                }
            }
            function hoverNavItem(top, duration) {
                Anim.stop("#J_CurrentBG", false, false, "switchAnim");
                $("#J_CurrentBG").css("width", "7px");
                new Anim("#J_CurrentBG", {
                        top: top
                    },
                    {
                        duration: duration,
                        easing: "easeNone",
                        queue: "switchAnim",
                        complete: function() {
                            new Anim("#J_CurrentBG", {
                                    width: "227px"
                                },
                                .3, "easeNone").run()
                        }
                    }).run()
            }
            function toggleWeibo() {
                var checked = $("#J_WeiboShare").hasAttr("checked");
                if (checked) {
                    $("#J_WeiboShare").removeAttr("checked");
                    $("#J_WeiboShareLink").removeClass("active");
                    $("#J_SyncComment").val("")
                } else {
                    $("#J_WeiboShare").attr("checked", "checked");
                    $("#J_WeiboShareLink").addClass("active");
                    $("#J_SyncComment").val("stid")
                }
            }
            function scrollCallback() {
                var scroll = $("window").scrollTop();
                if (scroll >= 500) {
                    $(".back-to-top").fadeIn(.5)
                } else {
                    $(".back-to-top").fadeOut(.3)
                }
                var bodyTop = $("body").offset().top;
                var navHeight = $("#nav").outerHeight();
                var contentHeight = $("#content").outerHeight();
                var diff = contentHeight - scroll - navHeight;
                if (diff <= 100) {
                    if (!$("#nav").hasClass("nav-1")) {
                        $("#nav").addClass("nav-1")
                    }
                    $("#nav").css("top", contentHeight + bodyTop - navHeight - 100)
                } else {
                    if ($("#nav").hasClass("nav-1")) {
                        $("#nav").removeClass("nav-1");
                        $("#nav").css("top", bodyTop)
                    }
                }
                if ($("#content").hasClass("content-single")) {
                    var $postContainer = $(".article-container");
                    var postHeight = $postContainer.outerHeight() + $postContainer.offset().top;
                    var barHeight = $(".entry-meta").outerHeight();
                    var screenHeight = $(".back-to-top").height();
                    if (postHeight + barHeight - scroll < screenHeight) {
                        var $entryMeta = $(".entry-meta");
                        if ($entryMeta.hasClass("entry-meta-fixed")) {
                            $entryMeta.removeClass("entry-meta-fixed")
                        }
                    } else {
                        var $entryMeta = $(".entry-meta");
                        if (!$entryMeta.hasClass("entry-meta-fixed")) {
                            $entryMeta.addClass("entry-meta-fixed")
                        }
                    }
                }
            }
            if (window.addEventListener) {
                window.addEventListener("scroll", scrollCallback, false);
                window.addEventListener("resize", scrollCallback, false)
            } else {
                window.attachEvent("onscroll", scrollCallback);
                window.attachEvent("onresize", scrollCallback)
            }
            scrollCallback();
            resetNavItem(true);
            if ($("#J_Slide").length) {
                var carousel = new Switchable.Carousel("#J_Slide", {
                    effect: "fade",
                    delay: 0,
                    duration: 1,
                    autoplay: true,
                    hasTriggers: false
                });
                $("#J_Pre").on("click",
                    function(e) {
                        carousel.prev();
                        return false
                    });
                $("#J_Next").on("click",
                    function(e) {
                        carousel.next();
                        return false
                    })
            }
            if ($(".articles li").length != 0) {
                S.each($(".articles li"),
                    function(item) {
                        if (readCookies($(item).attr("data-id")) == "true") {
                            $(item).one(".J_LikeSum").addClass("liked")
                        }
                    })
            }
            if ($("#content").hasClass("content-single")) {
                var postID = $("#J_PostID").val();
                if (readCookies(postID) == "true") {
                    $("#J_LikePost").addClass("liked")
                }
                S.each($(".entry-content img"),
                    function(item) {
                        $item = $(item);
                        $item.attr("data-width", $item.attr("width"));
                        $item.removeAttr("width");
                        $item.attr("data-height", $item.attr("height"));
                        $item.removeAttr("height")
                    })
            }
            $("#J_BackToTop").on("click",
                function(e) {
                    new Anim(window, {
                            scrollTop: "0"
                        },
                        1, "easeOutStrong",
                        function() {
                            $(".back-to-top").fadeOut(.3)
                        }).run();
                    return false
                });
            $("#J_Login").on("click",
                function(e) {
                    if (!$(this).hasClass("logged")) {
                        $("#form-wrap").slideToggle(.3, null, "")
                    }
                });
            $("#login").on("click",
                function(e) {
                    e.stopPropagation()
                });
            $("body").on("click",
                function(e) {
                    if (!$("#J_Login").hasClass("logged")) {
                        $("#form-wrap").slideUp(.3, null, "")
                    }
                });
            $("#J_MoreLinks").on("click",
                function(e) {
                    $("#J_LinksContainer").removeClass("heighted");
                    $(this).hide()
                });
            S.Event.delegate(document, "click", ".ks-ext-mask",
                function() {
                    $(".ks-ext-close").fire("click")
                });
            $(".entry-content img").on("click",
                function(e) {
                    $this = $(this);
                    window.open($this.attr("src"), $this.attr("title"));
                    return false
                });
            var navAnim;
            $(".photos-container").on("mouseover",
                function(e) {
                    if (navAnim) {
                        navAnim.stop(false)
                    }
                    navAnim = new Anim("#J_SlideNav", {
                            bottom: 0
                        },
                        .3, "easeOut");
                    navAnim.run()
                }).on("mouseleave",
                function(e) {
                    if (navAnim) {
                        navAnim.stop(false)
                    }
                    navAnim = new Anim("#J_SlideNav", {
                            bottom: "-40px"
                        },
                        .3, "easeIn");
                    navAnim.run()
                });
            $(".menu li").on("mouseover",
                function(e) {
                    e.stopPropagation();
                    var top = $(this).offset().top - $("#nav").offset().top - 100;
                    hoverNavItem(top, .1)
                });
            $(".menu-nav_menu-container").on("mouseleave",
                function() {
                    resetNavItem(false)
                });
            $(".like a").on("click",
                function(e) {
                    $this = $(this);
                    if ($this.hasClass("liked") || $this.hasClass("liking")) {
                        return false
                    }
                    $this.addClass("liking");
                    var reqUrl = $("#J_Home").val() + "/";
                    var postID = $this.parent(".post").attr("data-id");
                    var data = {
                        action: "like_post",
                        postID: postID
                    };
                    S.io({
                        url: reqUrl,
                        type: "post",
                        data: data,
                        dataType: "json",
                        success: function(msg) {
                            if (msg.stat == 200) {
                                writeCookies(msg.postID, true);
                                $this.removeClass("liking").addClass("liked");
                                $this.parent(".post").one(".J_LikeSum").html(msg.likeCount)
                            }
                        },
                        error: function() {
                            $this.removeClass("liking");
                            promot("Fail to like post!")
                        }
                    });
                    return false
                });
            $("#J_CommentPost").on("click",
                function(e) {
                    S.DOM.scrollIntoView("#J_CommentAnchor", window);
                    return false
                });
            $(".comment .J_Reply").on("click",
                function(e) {
                    S.DOM.scrollIntoView("#J_RespondAnchor", window);
                    var $comment = $(this).parent(".comment");
                    var authorToReply = $comment.attr("data-username");
                    $("#comment_parent").val($comment.attr("data-id"));
                    $("#J_Comment").attr("placeholder", "回复" + authorToReply + ": ").fire("focus");
                    $("#J_AuthorToReply").html("@" + authorToReply).attr("title", "回复 " + authorToReply);
                    $("#J_DelReplyTo").attr("title", "取消回复");
                    $("#J_ReplyTo").fadeIn()
                });
            $("#J_DelReplyTo").on("click",
                function(e) {
                    $("#J_ReplyTo").fadeOut(.3,
                        function() {
                            $("#J_Comment").attr("placeholder", "说点什么吧...");
                            $("#comment_parent").val("0");
                            $("#J_DelReplyTo").removeAttr("title");
                            $("#J_AuthorToReply").html("").removeAttr("title")
                        },
                        "easeOut")
                });
            $("#J_WeiboShare, #J_WeiboShareLink").on("click",
                function(e) {
                    toggleWeibo()
                });
            S.each($(".wp-post-image"),
                function(item) {
                    var $item = $(item);
                    var img = new Image;
                    img.onload = function() {
                        $item.attr("src", $(this).attr("src")).fadeIn()
                    };
                    img.src = $item.attr("data-src");
                    if (img.complete) {
                        $item.attr("src", img.src).fadeIn()
                    }
                })
        })
});
